package com.drama.api.sys.version.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.drama.api.base.ApiIndex;
import com.drama.api.base.Constants;
import com.drama.api.base.R;
import com.drama.api.base.RestPage;
import com.drama.api.sys.version.domain.vo.SysVersionAddVO;
import com.drama.api.sys.version.domain.vo.SysVersionSearchVO;
import com.drama.api.sys.version.domain.vo.SysVersionVO;
import com.drama.api.sys.version.service.ISysVersionService;
import com.drama.util.BindingResultUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;


@Api( tags = ApiIndex.SysVersionController)
@RequestMapping(value = "/api/sys/version")
@RestController
@SaCheckLogin
public class SysVersionController {


    @Autowired
    private ISysVersionService service;


    @ApiOperation(value = "查询版本更新列表")
    @GetMapping(value = "/list")
    public R<RestPage<List<SysVersionVO>>> list(
            @ApiParam(value = "查询参数") @ModelAttribute SysVersionSearchVO searchVO) {
        List<SysVersionVO> list = service.getList(searchVO);
        RestPage restPage = new RestPage( new PageInfo(list),list);
        return new R(restPage);
    }

    @ApiOperation(value = "创建版本更新")
    @PostMapping(value = "/add")
    public R create(@ApiParam(value = "添加参数") @Valid @RequestBody SysVersionAddVO vo, BindingResult bindingResult) {
        R result= BindingResultUtil.BindingResult(bindingResult);
        if(result.getCode()== Constants.SYS_SUCCESS){
            service.add(vo);
        }
        return result;
    }


    @ApiOperation(value = "更新前端页面")
    @PostMapping(value = "/update_web")
    @ResponseBody
    public R upload(
            @ApiParam(value = "文件【需要是zip包】", required = true) @RequestParam("file") MultipartFile file
    ) throws IOException {
        return service.updateWeb(file);
    }



}
